﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using FilStartUps.Helper;
using System.IO;

namespace FilStartUps
{
    public partial class ForgotPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected void btnSend_Click(object sender, EventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var member = db_connect.Members
                                .Where(m => m.emailaddress.Equals(txtemail.Text.Trim())
                                        && m.secretquestion.Equals(ddlsecurityquestion.SelectedValue)
                                        && m.secretanswer.Equals(txtsecretanswer.Text.Trim())
                                        && m.member_flag.Equals(true)).SingleOrDefault();
                if (member != null)
                {
                    var member_ = db_connect.Members.Where(m => m.memberidPK.Equals(member.memberidPK)).SingleOrDefault();
                    //Generate Random Text for password substring 10;
                    string password =GetRandomString();
                    member_.password =  Helper.Helper.HashPassword(password);
                    member_.confirmpassword = Helper.Helper.HashPassword(password);
                    db_connect.Entry(member_).State = System.Data.EntityState.Modified;
                    db_connect.SaveChanges();
                    Helper.Emailer.SendForgotPassword(member_.emailaddress, password);
                   
                }
                else
                {
                  
                }
                //Notification
            }
        }
        public static string GetRandomString()
        {
            string path = Path.GetRandomFileName();
            path = path.Replace(".", "");
            return path;
        }
    }
}